﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using IDT.Entity;
using Image = System.Drawing.Image;
using System.Threading;
using System.Xml.Serialization;
using IDT.Downloader;

namespace IDT.Wpf.Watcher
{
    /// <summary>
    /// Interaction logic for DownloadImageWindow.xaml
    /// </summary>
    public partial class LoadWebSchemaWindow : Window
    {
        private ImageDownloader imageDownloader;

        public LoadWebSchemaWindow()
        {
            InitializeComponent();
            ListBoxCategories.ItemsSource = CategoryCollection;
            ListBoxAlbums.ItemsSource = AlbumCollection;
        }

        public LoadWebSchemaWindow(ImageDownloader imageDownloader) : this()
        {
            this.imageDownloader = imageDownloader;
        }

        public ObservableCollection<Category> CategoryCollection { get; } = new ObservableCollection<Category>();

        public ObservableCollection<Album> AlbumCollection { get; } = new ObservableCollection<Album>();

        private async void DoButtonStartClicked(object sender, RoutedEventArgs e)
        {
            CategoryCollection categories = await DownloadWholeWebsiteImages();
            if (categories != null)
            {
                string xmlpath = $"{imageDownloader.WebInfo.HomeUrl.TrimStart("http://".ToCharArray())}.xml";
                FileStream fs = new FileStream(xmlpath, FileMode.Create, FileAccess.Write);
                XmlSerializer serializer = new XmlSerializer(typeof(CategoryCollection));
                serializer.Serialize(fs, categories);
                fs.Close();

                MessageBox.Show("Download whole website images successfully.", "Download Result", MessageBoxButton.OK,
                    MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("Download whole website images faild.", "Download Result", MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
        }


        private async Task<CategoryCollection> DownloadWholeWebsiteImages()
        {
            CategoryCollection result = null;

            try
            {
                CategoryCollection categories = await imageDownloader.GetCategories();
                ProgressBarCategoriesDownloaded.Maximum = categories.Count;
                ProgressBarCategoriesDownloaded.Value = 0;

                foreach (Category category in categories)
                {
                    List<Album> albums = await imageDownloader.GetAlbumsForCategory(category);
                    ProgressBarAlbumsDownloaded.Maximum = albums.Count;
                    ProgressBarAlbumsDownloaded.Value = 0;

                    foreach (Album album in albums)
                    {
                        await imageDownloader.GetPhotosForAlbum(album);
                        AlbumCollection.Add(album);
                        ProgressBarAlbumsDownloaded.Value++;
                    }

                    CategoryCollection.Add(category);
                    ProgressBarCategoriesDownloaded.Value++;
                }

                result = categories;
            }
            catch (Exception)
            {
                // ignored
            }

            return result;
        }

        private void DoButtonClearClicked(object sender, RoutedEventArgs e)
        {
            CategoryCollection.Clear();
            AlbumCollection.Clear();
        }

        private void DoButtonCloseClicked(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
